package org.tigris.atlas.xml.schema.mda.generator;

import java.util.Iterator;

import org.apache.velocity.VelocityContext;
import org.tigris.atlas.mda.generator.AbstractResourcesGenerator;
import org.tigris.atlas.mda.generator.GenerationContext;
import org.tigris.atlas.mda.generator.GenerationException;
import org.tigris.atlas.mda.metadata.MetadataRepository;
import org.tigris.atlas.mda.metadata.element.Entity;
import org.tigris.atlas.xml.schema.mda.element.SchemaElementUtils;
import org.tigris.atlas.xml.schema.mda.element.SchemaEntity;

public class SchemaEntityMaintenanceServiceResponseGenerator extends AbstractResourcesGenerator {
	
	public void generate(GenerationContext context) throws GenerationException {
		String applicationName = context.getArtifactId();
		Iterator entities = MetadataRepository.getInstance().getAllEntities(applicationName)
				.values().iterator();

		String fileName;
		String basefileName = context.getOutputFile();
		basefileName = replaceBasePackage(basefileName, context.getBasePackageAsPath());
		while (entities.hasNext()) {
			Entity entity = (Entity) entities.next();
			SchemaEntity schemaEntity = new SchemaEntity(entity);

			// this template is also used to generate responses for entities
			// from the
			// entity maintenance server, so the properties are a little more
			// abstract:
			VelocityContext vc = new VelocityContext();
			vc.put("responseName", schemaEntity.getName());
			vc.put("entity", schemaEntity);
			vc.put("basePackage", context.getBasePackage());

			String baseNamespace = SchemaElementUtils.translateBasePackageToBaseSchemaLocation(context.getBasePackage());
			vc.put("baseNamespace", baseNamespace);
			vc.put("serviceResponseQualifier", "ServiceResponse");

			fileName = replaceEntityName(basefileName, entity.getName());
			context.setOutputFile(fileName);

			generateFile(context, vc);
		}
	}

}
